var config = require('../libs/config.js');

function sendGetRequest(methodName, data,success,fail) {
  wx.showLoading({ title: "加载中..", mask: true })
        wx.request({
            url:  config.Config.serverAddress+methodName,
            data: data,
            method: 'GET',
            header: {'authorization':getApp().globalData.token},//userInfo.authorization
            success: function(res){
              wx.hideLoading();
              switch(res.statusCode){
                case 200:{
                  if (typeof success == "function") {
                    success(res);
                  }
                } break;
                case 401:{
                  getApp().login();
                }
                default:{
                    wx.showToast({
                        title: '系统错误,请联系客服',
                        icon: 'none',
                        duration: 1000
                    })
                }

              }
            },
            fail: function(res){
                if(typeof fail == "function"){
                    fail(res);
                }
            }
        })
};

function sendPostRequest(methodName, data,success,fail) {
      wx.showLoading({ title: "加载中..", mask: true })
        wx.request({
            url:  config.Config.serverAddress+methodName,
            data: data,
            method: 'POST',
            header: {'content-type':'application/x-www-form-urlencoded'
                ,'authorization':getApp().globalData.token},//userInfo.authorization
            success: function(res){
              wx.hideLoading();
                if(typeof success == "function"){
                    success(res);
                }
            },
            fail: function(res){
                if(typeof fail == "function"){
                    fail(res);
                }
            }
        })
};

module.exports={
    sendGetRequest : sendGetRequest,
    sendPostRequest:sendPostRequest
}